<?php

abstract class CBaseActiveRecord extends CActiveRecord
{
    public $photo;
    public $photos;

	public function __construct($scenario = 'insert')
	{
		parent::__construct($scenario);
	}

    public function setPhoto($photo)
    {
        $this->photo = $photo;
    }

    public function getPhoto()
    {
        return $this->photo;
    }

    public function setPhotos($photos)
    {
        $this->photos = $photos;
    }

    public function getPhotos()
    {
        return $this->photos;
    }


    public function getUrlId($url)
    {
        return substr($url, 0, strpos($url, '-'));
    }

    public function createUrlId()
    {
        $id = $this->id;
        $id .= '-';

        if(isset($this->alias))
            $id .= $this->alias;
        else
            $id .= Transliteration::text($this->title);

        return $id;
    }

	public function createAlias($title)
	{
		$title = Transliteration::text($title);
		$title = mb_eregi_replace('[^a-zА-ЯіЇ0-9\-]', '-', $title);
		$title = mb_eregi_replace('\-{2,}', '-', $title);

		return mb_strtolower($title);
	}

	/**
	 * @param $id - parent_id in db
	 * @return $this
	 */
	public function id($id)
	{
		if($id < 1)
			return $this;

		$this->getDbCriteria()->addInCondition('id', (array)$id);

		return $this;
	}

	public function limit($limit)
	{
		if($limit < 1)
			return $this;

		$this->getDbCriteria()->limit = $limit;

		return $this;
	}

	public function orderByRand()
	{
		$this->getDbCriteria()->order = 'RAND()';

		return $this;
	}

	public function order($order)
	{
		$this->getDbCriteria()->order = $order;

		return $this;
	}

	public function group($group)
	{
		$this->getDbCriteria()->group = $group;

		return $this;
	}

	public function pager(CPagination $oPager)
	{
//		$oPager->setItemCount($this->count(clone $this->getDbCriteria()));
		$oPager->applyLimit($this->getDbCriteria());

//		print_r($this->getDbCriteria()); exit;

		return $this;
	}

	public function filter(array $filters)
	{
		$this->getDbCriteria()->addColumnCondition($filters);

		return $this;
	}

	public function getTableName()
	{
		if(strpos($this->tableName(), '.') !== false)
			return substr($this->tableName(), strpos($this->tableName(), '.')+1);

		return $this->tableName();
	}

	public function toTree(array $rows, $idName, $pidName)
	{
		$children = array(); // children of each ID
		$ids = array();
		// Collect who are children of whom.
		foreach ($rows as $i=>$r) {
			$row =& $rows[$i];
			$id = $row[$idName];
			if ($id === null) {
				// Rows without an ID are totally invalid and makes the result tree to
				// be empty (because PARENT_ID = null means "a root of the tree"). So
				// skip them totally.
				continue;
			}
			$pid = $row[$pidName];
			if ($id == $pid) $pid = null;
			$children[$pid][$id] =& $row;
			if (!isset($children[$id])) $children[$id] = array();
			$row->childNodes =& $children[$id];
			$ids[$id] = true;
		}

		// Root elements are elements with non-found PIDs.
		$forest = array();
		foreach ($rows as $i=>$r) {
			$row =& $rows[$i];
			$id = $row[$idName];
			$pid = $row[$pidName];
			if ($pid == $id) $pid = null;
			if (!isset($ids[$pid])) {
				$forest[$row[$idName]] =& $row;
			}
//            unset($row[$idName]);
//            unset($row[$pidName]);
		}
//        print_r($forest); exit;
		return $forest;
	}

	public function generateAlias($id, $title)
	{
		if(!empty($title))
		{
			$alias = $this->createAlias($title);

			$index = 0;
			do
			{
				$index++;

				$params = array();
				
				if($id > 0)
				{
					$params = array(
						'condition' => 'id != :id',
						'params' => array(':id' => $id)
					);
				}

				$oTmpRow = $this->findByAttributes(
					array('alias' => $alias),
					$params
				);

				if($oTmpRow)
					$alias = $this->createAlias($title ."_". $index);
			}
			while($oTmpRow);

			return trim($alias, '-');
		}

		return '';
	}

}